package com.cc.sanjieduan.interceptors;

import com.cc.sanjieduan.cache.MenuCache;
import com.cc.sanjieduan.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ApiCheckInter implements HandlerInterceptor {
    private MenuCache menuCache;

    @Autowired
    public void setMenuCache(MenuCache menuCache) {
        this.menuCache = menuCache;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取到请求地址
        String requestURI = request.getRequestURI();//请求的地址
        //查看这个地址是不是被禁用了
        System.err.println("请求地址:"+requestURI);
        Menu menu = menuCache.getMenuByUrl(requestURI);
        if (menu != null) {
            String enable = menu.getEnable();//获取是否启用
            //如果禁用了就返回false 可以返回数据
            if (!"1".equals(enable)) {
                response.getWriter().write("lan jie le");
            }
            return "1".equals(enable);
        }

        return true;
    }
}
